package com.say.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.say.bean.common.Response;
import com.say.bean.common.SplitPage;
import com.say.bean.domain.Trace;
import com.say.bean.domain.User;
import com.say.service.MessageService;
import com.say.service.TraceService;
import com.say.utils.DateHelper;
import com.say.utils.UserUtil;

@Controller
@RequestMapping("/wensay/message")
public class ApiMessageController extends BaseController {
	
    @Autowired
    public MessageService messageService;
    
    @Autowired
    public TraceService traceService;
	
	//信息列表页面
	@RequestMapping()
	public String index(Model model){
		User user = UserUtil.getUser(request);
		String userId=user.getId();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
       
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(1);
        sp.setPageSize(100);
        sp.setParams(params);
        Page<Map<String, Object>> page = messageService.findMessageByPage(params, initPage(1,100));
        List<Map<String, Object>> list=page.getContent();
        for (Map<String, Object> map : list) {
        	map.put("time",DateHelper.format(DateHelper.parseDate(map.get("update_time").toString(), "yyyy-MM-dd HH:mm:ss"),"HH:mm"));
		}
        
        model.addAttribute("list", list);
        model.addAttribute("user", user);
        model.addAttribute("trace", traceService.getTraceByFriendsUserId(userId));
        traceService.save(new Trace(user.getId(),"0.0.0.0","访问信息列表页面"));
		return "wensay/message";
	}
	
	@RequestMapping("/interval/page")
	@ResponseBody
    public Response intervalPage(Model model,String toUserId,String maxChatId) {
		Response response = new  Response();
		User user = UserUtil.getUser(request);
		String userId=user.getId();
		Map<String, Object> params = new HashMap<String, Object>();
	    params.put("userId", userId);
        Page<Map<String, Object>> page = messageService.findMessageByPage(params, initPage(1,10));
        List<Map<String, Object>> list=page.getContent();
        for (Map<String, Object> map : list) {
        	map.put("time",DateHelper.format(DateHelper.parseDate(map.get("update_time").toString(), "yyyy-MM-dd HH:mm:ss"),"HH:mm"));
		}
        response.success(list,"查询成功");
		return response;
    }

	
	


}
